/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.datastructure;

import edu.yonsei.iwb.sdt.datastructure.tree.DEdge;
import edu.yonsei.iwb.sdt.datastructure.tree.DNode;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import edu.yonsei.iwb.sdt.refinement.Refinement;
import java.util.Vector;
import edu.yonsei.iwb.sdt.ontology.PropertyArc;

/**
 *
 * @author JDK
 */
public class DecomposeCandidate {

    private int refinement_type;
//    private Resource topclass;
    private Vector<DEdge> edgeSet = new Vector<DEdge>();
    private Vector<Refinement> refset;
    private double information_gain;
    private PropertyArc pa;
    private Vector<DNode> childNodeSet;    

    public DecomposeCandidate(Vector<Refinement> refset, int rtype) {
        this.refinement_type = rtype;
        this.refset = refset;
        this.generateNode();
        
//        this.generateSubTree(refset);
    }

    public DecomposeCandidate(Vector<Refinement> refset, PropertyArc pa, int rtype) {
        this.refinement_type = rtype;
        this.pa = pa;
        this.refset = refset;
        this.generateNode();
        
//        this.generateSubTree(refset);
    }
    
    public int getExpansionDepth(){
        
        try{
            return this.refset.firstElement().getExpandingHistory().getPropertySet().size();            
        }catch(NullPointerException npe){
            return 0;
        }        
    }

    public String getProperty() {
        return this.pa.getProperty();
    }

    public int getRefinementType() {
        return this.refinement_type;
    }

    public PropertyArc getPropertyArc() {
        return pa;
    }
    
    public void setInformation_Gain(double s) {
        this.information_gain+=s;
    }

    public double getInformation_Gain() {
        return this.information_gain;
    }

    public Vector<Refinement> getRefinementSet() {
        return this.refset;
    }

    public Vector<DNode> getChildNodeSet() {
        return this.childNodeSet;
    }

    private void generateNode() {
        this.childNodeSet = new Vector<DNode>();
        for (int i = 0; i < this.refset.size(); i++) {
            Refinement ref = this.refset.get(i);
            ref.generateRefinementName();
            
//            System.out.println("- Refinement name -");
//            System.out.println(ref.toString());
//            UtilFunctions.printVectorOfResource(ref.getInstanceSet());            
//            System.out.println("=====================");
            
            DNode cn = new DNode(ref.getInstanceSet(), ref.toString());
            this.childNodeSet.add(cn);
        }
    }

    public Vector<DEdge> generateEdge(DNode parentNode) {

        for (int i = 0; i < this.refset.size(); i++) {
            Refinement ref = this.refset.get(i);
            ref.generateRefinementName();
//            Node cn = new Node(ref.getInstanceSet());
            DNode childNode = this.childNodeSet.get(i);
//            childNode.setRuleComposer(parentNode.getRuleComposer(), ref);
            DEdge e = new DEdge(parentNode, childNode, ref);
            this.edgeSet.add(e);
            parentNode.setEdge(e);

        }

        return this.edgeSet;
    }
    
    public String explanation(){
        String s = new String();        
        for (int i = 0; i < this.refset.size(); i++) {
            s+="Refinement " + i + " : " + this.refset.get(i).toString()+"\n";
        }
        s += "IG = " + this.information_gain;
        return s;
    }
    
    public void printDecomposeCandidateDetail(){
        System.out.println("refinement type : " + this.refinement_type);
        for (int i = 0; i < this.refset.size(); i++) {
            System.out.println("Refinement " + i + " : " + this.refset.get(i).toString());
        }
//        System.out.println("information gain : " + this.information_gain);
//        System.out.println("--------------------------------------------");
    }

//    public void calculateInformationGain(Node parentNode, TargetInstanceSet tis) {
//        
//        for (int i = 0; i < this.childNodeSet.size(); i++) {
//            Node childnode = this.childNodeSet.get(i);
//            
//        }
//
//
//
//    }
    
}
